package com.whut.producer.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.whut.producer.pojo.Data_WD;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author WZH
 * @create 2021-08-27 10:01
 * @desc 消息生产者接口
 **/
@RestController
@RequestMapping("/producer")
public class MqProducerController {
    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("/send")
    public JSONArray send(){
        JSONObject result = new JSONObject();
        /*
        生产100个数据，并存入一个List中
        */
        ArrayList<Data_WD> WD_List = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
            /*
            模拟65个温度数据，存入一个List中
             */

            for (int j = 0; j < 65; j++) {
                Data_WD data_wd=new  Data_WD(sdf.format(new Date()),Math.random()*10,"WD"+Math.round(Math.random()*1000));
                WD_List.add(data_wd);
            }

            rocketMQTemplate.convertAndSend("DATA_WD",WD_List );
        }
        return JSON.parseArray(WD_List.toString());
    }
}
